<?php
class SlideController extends AdminController{
    public function indexAction(){
        $model = new SlideModel;
        $data = $model->select();        
        $this->_view->slide = $data;
    }
    public function createAction(){}
    public function insertAction(){
        Yaf_Dispatcher::getInstance()->disableView();
        $names = [
            'title'=>6,
            'link'=>6,
            'style'=>6,
            'description'=>6,
            'sign'=>6,
            'sort'=>3,
            'picture'=>6
        ];
        $data = Request::batch(1,$names);
        $model = new SlideModel;
        $flag = $model->insert($data);
        if(empty($flag)){
            Response::error('error');
        }else{
            Response::success('nice job');
        }
    }
    public function editAction(){
        $id = Request::asget(3,'id',null);
        if($id == null){
            return false;
        }else{
            $where = ['id'=>$id];
            $model = new SlideModel;
            $data = $model->where($where)->one();
            $this->_view->slide = $data;
        }        
    }
    public function updateAction(){
        Yaf_Dispatcher::getInstance()->disableView();
        $id = Request::aspost(3,'id',0);
        if($id == 0){
            return false;
        }else{
            $names = [
                'title'=>6,
                'link'=>6,
                'style'=>6,
                'description'=>6,
                'sign'=>6,
                'sort'=>3,
                'picture'=>6
            ];
            $data = Request::batch(1,$names);
            $model = new SlideModel;
            $flag = $model->where(['id'=>$id])->update($data);
            if(empty($flag)){
                Response::error('error');
            }else{
                Response::success('nice job');
            }
        }
    }
    public function statusAction(){
        Yaf_Dispatcher::getInstance()->disableView();
        $id = Request::aspost(3,'id','');
        if(empty($id)){
            die;
        }else{
            $status = isset($_POST['status']) ? $_POST['status'] : '';
            if($status == ''){
                return false;
            }else{
                switch($status){
                case '0': $data['status'] = 1;
                    break;
                case '1': $data['status'] = 0;
                    break;
                }
                $model = new SlideModel;
                $flag = $model->where(['id'=>$id])->update($data);
                if($flag == 0 || $flag == false){
                    Response::error('Wrong');
                }else{
                    Response::success('Nice');
                }
            }
        }        
    }
    public function uploadeAction(){
        Yaf_Dispatcher::getInstance()->disableView();
        $up = new Upload;
        $up->save_path = 'slide';
        $file = $up->upload();
        Response::work($file);
    }    
}